package cn.expopay.mail;

import cn.expopay.mail.exception.MailException;
import cn.expopay.mail.model.Mail;
import cn.expopay.mail.util.MailUtil;
import org.apache.commons.lang3.StringUtils;

/**
 * 邮件API
 *
 * Created by JJ on 2016/8/29.
 */
public class MailApi {

    /**
     *
     * @param host:邮件服务器
     * @param sender：发件人账号
     * @param recievers：接收人数组
     * @param userName：发件人用户名
     * @param password：发件人密码
     * @param name：发件人姓名
     * @param subject：主题
     * @param message：邮件内容
     */
    public static void sendMail(String host,String sender,String userName,String password,String name,String[] recievers,String subject,String message) throws Exception{
        boolean result = checkEmpty(host,sender,userName,password,name,recievers,subject,message);
        if(result == true){
            throw new MailException("输入参数不完整！");
        }
        Mail mail = new Mail();
        mail.setHost(host);
        mail.setSender(sender);
        mail.setName(name);
        mail.setReceivers(recievers);
        mail.setUsername(userName);
        mail.setPassword(password);
        mail.setSubject(subject);
        mail.setMessage(message);
       try{
           new MailUtil().send(mail);
       }catch (Exception e){
           throw new MailException(e.getMessage());
       }
    }

    /**
     * 非空检查
      * @param host
     * @param sender
     * @param userName
     * @param password
     * @param name
     * @param recievers
     * @param subject
     * @param message
     */
    private static boolean checkEmpty(String host, String sender, String userName, String password, String name, String[] recievers, String subject, String message) {
        if(StringUtils.isEmpty(host) || StringUtils.isEmpty(sender) || StringUtils.isEmpty(userName)
                || StringUtils.isEmpty(password) || StringUtils.isEmpty(name) || StringUtils.isEmpty(subject)
                || StringUtils.isEmpty(message) || recievers == null || recievers.length < 0){
            return true;
        }else {
            return  false;
        }
    }

}
